package cn.foodProject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.squareup.picasso.Picasso;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.URL;

import cn.foodProject.R;
import cn.foodProject.bean.MyOkhttp;
import cn.foodProject.bean.MyGson;
import cn.foodProject.bean.MyUrl;
import cn.foodProject.gsonBean.FoodDetailsGsonBean;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FoodDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "FoodDetailsActivity";
    private Toolbar toolbarFood;
    private ImageView imgFoodDetails;
    private TextView tvFoodDetailsName;
    private TextView tvFoodDetailsPrice;
    private TextView tvFoodDetailsDescription;
    private Button btnBuy;
    private Button btnCommentList;
    private final Context context = FoodDetailsActivity.this;
    private int foodId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_details);
        //获取上一个活动传来的foodId
        Intent intent = getIntent();
        foodId = intent.getIntExtra("foodId", 0);
        initView();
        new Thread(){
            @Override
            public void run() {
                super.run();
                updateDetails(foodId);
            }
        }.start();
    }

    private void initView() {
        toolbarFood = (Toolbar) findViewById(R.id.toolbar_food);
        imgFoodDetails = (ImageView) findViewById(R.id.img_food_details);
        tvFoodDetailsName = (TextView) findViewById(R.id.tv_food_details_name);
        tvFoodDetailsPrice = (TextView) findViewById(R.id.tv_food_details_price);
        tvFoodDetailsDescription = (TextView) findViewById(R.id.tv_food_details_description);
        btnBuy = (Button) findViewById(R.id.btn_buy);
        btnCommentList = (Button) findViewById(R.id.btn_comment_list);

        //toolbar相关
        setSupportActionBar(toolbarFood);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbarFood.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        btnBuy.setOnClickListener(this);
        btnCommentList.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击购买时
            case R.id.btn_buy:
                Toast.makeText(context, "感谢购买!", Toast.LENGTH_SHORT).show();
                finish();
                break;

            //点击评论列表时,传递餐品id并跳转至当前餐品的评论列表
            case R.id.btn_comment_list:
                Intent intent = new Intent(context, CommentActivity.class);
                intent.putExtra("foodId", foodId);
                startActivity(intent);
                break;
        }
    }

    public void updateDetails(Integer foodId) {
        //拼接url
        URL url = MyUrl.getUrl("/food/findone/" + foodId);
        try {
            Response response = MyOkhttp.getResponse(url);
            //当请求成功时
            if (response.isSuccessful()) {
                String string = response.body().string();
                final FoodDetailsGsonBean foodDetailsGsonBean = (FoodDetailsGsonBean) MyGson.fromJson(string, FoodDetailsGsonBean.class);

                if (foodDetailsGsonBean.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //获取当前食物的详细信息并更新ui
                            Picasso.with(context)
                                    .load(MyUrl.MY_URL + "/" + foodDetailsGsonBean.getData().getIcon())
                                    .into(imgFoodDetails);
                            tvFoodDetailsName.setText(foodDetailsGsonBean.getData().getName());
                            tvFoodDetailsDescription.setText(foodDetailsGsonBean.getData().getDescription());
                            tvFoodDetailsPrice.setText(foodDetailsGsonBean.getData().getPrice()+"");
                        }
                    });
                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, foodDetailsGsonBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } else {
                Log.d(TAG, "onFailure: 请求失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "失败!请检查输入或网络!", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
